package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.ClassCircleDao;
import com.example.mapper.CircleLikesMapper;
import com.example.mapper.ClassCircleMapper;
import com.example.mapper.TeacherMapper;
import com.example.pojo.CircleLikes;
import com.example.pojo.Classes;
import com.example.pojo.ClassesCircle;
import com.example.pojo.TeacherInfo;
import com.example.pojo.dto.TeacherCircleDTO;
import com.example.service.ClassCircleService;
import com.example.service.TeacherInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ClassCircleServiceImpl implements ClassCircleService {
    @Resource
    ClassCircleMapper classCircleMapper;
    @Resource
    TeacherMapper teacherMapper;

    @Resource
    CircleLikesMapper circleLikesMapper;

    @Override
    public List<TeacherCircleDTO> getClassesCirclesByClassId(Long classId) {
        if (classId == null) {
            throw new IllegalArgumentException("classId cannot be null");
        }

        List<ClassesCircle> classesCircles = classCircleMapper.selectByClassId(classId);

        if (classesCircles == null) {
            throw new NullPointerException("classesCircles list is null");
        }
        classesCircles.forEach(circle -> {
            if (circle != null) {
                List<CircleLikes> likes = circleLikesMapper.selectByClassesCircleId(circle.getClassesCircleId());
                circle.setCircleLikes(likes);
            }
        });
        // 首先查询出班级圈信息
        return convertToDTO(classesCircles);
    }

    @Override
    public int deleteCircleById(ClassesCircle circle) {
        return classCircleMapper.deleteById(circle);
    }

    @Override
    public int likeClassCircle(Long likesId, Long classesCircleId) {
        CircleLikes circleLikes = new CircleLikes();
        circleLikes.setLikesId(likesId);
        circleLikes.setClassesCircleId(classesCircleId);
        circleLikes.setLikedAt(LocalDateTime.now());
        return circleLikesMapper.insert(circleLikes);
    }

    @Override
    public int cancelLikeClassCircle(Long likesId, Long classesCircleId) {
        QueryWrapper<CircleLikes> circleLikesQueryWrapper = new QueryWrapper<>();
        circleLikesQueryWrapper.eq("likes_id", likesId).eq("classes_circle_id", classesCircleId);
        return circleLikesMapper.delete(circleLikesQueryWrapper);
    }

    @Override
    public int addClassCircle(ClassesCircle circle) {
        circle.setCreatedAt(LocalDateTime.now());
        return classCircleMapper.insert(circle);
    }

    private List<TeacherCircleDTO> convertToDTO(List<ClassesCircle> classesCircles) {
        if (classesCircles == null || classesCircles.isEmpty()) {
            return new ArrayList<>();
        }

        List<TeacherCircleDTO> teacherCircleDTOs = new ArrayList<>();

        for (ClassesCircle circle : classesCircles) {
            if (circle != null) {
                Long authorId = circle.getAuthorId();

                if (authorId != null) {
                    TeacherInfo teacherInfo = teacherMapper.selectById(authorId);

                    if (teacherInfo != null) {
                        TeacherCircleDTO dto = new TeacherCircleDTO(authorId, teacherInfo.getTeacherName(), teacherInfo.getTeacherAvatar(), circle);
                        teacherCircleDTOs.add(dto);
                    }
                }
            }
        }
        return teacherCircleDTOs;
    }
}
